package fr.uhp.mpr.data;
import java.util.*;
import  java.text.*;
import java.io.*;

/**
 * Description de l'organisation des donnees contenues dans le fichier.
 */
public class DataMeter {

	public static enum iname{TEMPERATURE,HUMIDITY,PRESSURE,RAIN,WIND,DEWPOINT,WINDTEMP,SQUALL};

	private static final DateFormat df = new SimpleDateFormat("dd/MM/yy");

	private static final Intervalle[] INTERTEMPERATURE =  {
		new Intervalle(Double.NEGATIVE_INFINITY,-5,false,false),
		new Intervalle(-5,0),
		new Intervalle(0,5),
		new Intervalle(5,10),
		new Intervalle(10,15),
		new Intervalle(15,20),
		new Intervalle(20,25),
		new Intervalle(25,Double.POSITIVE_INFINITY)
	},

	INTERHUMIDITE =  {
		new Intervalle(0,0.4),
		new Intervalle(0.4,0.5),
		new Intervalle(0.5,0.6),
		new Intervalle(0.6,0.7),
		new Intervalle(0.7,0.8),
		new Intervalle(0.8,0.9),
		new Intervalle(0.9,1,true,true)
	},

	INTERPRESSION =  {
		new Intervalle(Double.NEGATIVE_INFINITY,990,false,false),
		new Intervalle(990,1000),
		new Intervalle(1000,1010),
		new Intervalle(1010,1020),
		new Intervalle(1020,1030),
		new Intervalle(1030,1040),
		new Intervalle(1040,Double.POSITIVE_INFINITY,true,true)
	},

	INTERPLUIE =  {
		new Intervalle(0,0,true,true),
		new Intervalle(0,2,false,true),
		new Intervalle(2,Double.POSITIVE_INFINITY,false,false)
	},

	INTERVENT =  {
		new Intervalle(0,2.5),
		new Intervalle(2.5,5),
		new Intervalle(5,7.5),
		new Intervalle(7.5,10),
		new Intervalle(10,12.5),
		new Intervalle(12.5,15),
		new Intervalle(15,Double.POSITIVE_INFINITY)
	},

	INTERROSEE =  {
		new Intervalle(Double.NEGATIVE_INFINITY,-10,false,false),
		new Intervalle(-10,-5),
		new Intervalle(-5,0),
		new Intervalle(0,5),
		new Intervalle(5,10),
		new Intervalle(10,15),
		new Intervalle(15,Double.POSITIVE_INFINITY)
	},

	INTERRAFALES =  {
		new Intervalle(0,5),
		new Intervalle(5,10),
		new Intervalle(10,15),
		new Intervalle(15,20),
		new Intervalle(20,25),
		new Intervalle(25,30),
		new Intervalle(30,Double.POSITIVE_INFINITY)
	};

	private ArrayList<Jour> data;


	public DataMeter() {
		this.data = importData();
	}

	public static Intervalle[] getIntervalle(iname inter) {
		switch (inter) {
		case TEMPERATURE : return INTERTEMPERATURE;
		case HUMIDITY    : return INTERHUMIDITE;
		case PRESSURE    : return INTERPRESSION;
		case RAIN        : return INTERPLUIE;
		case WIND        : return INTERVENT;
		case DEWPOINT    : return INTERROSEE;
		case WINDTEMP    : return INTERTEMPERATURE; 
		case SQUALL      : return INTERRAFALES;
		}
		return null;
	}

	public static Intervalle[] getIntervalle(int index) {
		return getIntervalle(iname.values()[index]);
	}

	public static Intervalle[] getIntervalle(String str) {
		return getIntervalle(iname.valueOf(str));
	}

	/**
	 * Creation d'un calendrier meteo a partir du fichier source.
	 * @return meteo d'une liste de jour
	 */
	 public static ArrayList<Jour> importData() {
		ArrayList<Jour> data = new ArrayList<Jour>();
		try{
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("mpr_projet_data.txt")));
			String ligne;
			int count = 0;
			while ((ligne=br.readLine())!=null){
				if(count > 4){
					data.add(createJour(ligne));
				}else count++;
			}
			br.close(); 
		}catch(Exception e){ System.out.println("importData error"); }

		return data;
	}

	/**
	 * Determine l'intervalle contenant la donnee data.
	 * @param data
	 * @param inter
	 * @return -1 en cas d'echec, l'intervalle ou se situe la donnee sinon
	 */
	 public static int findCorrectIntervalle(double data, Intervalle[] inter) {
		for (int i = 0; i < inter.length; i++)
			if (inter[i].contains(data))
				return i;
		return -1;
	}

	public static int findCorrectIntervalle(String string, Intervalle inter) {
		Intervalle[] tab = DataMeter.getIntervalle(string);

		for (int i = 0; i < tab.length; i++)
			if (inter.compare(tab[i]))
				return i;

		return -1;
	}


	private static Jour createJour(String line){
		String[] tab = line.split("\t");
		if(tab.length == 10){   
			Jour j = new Jour(
					extractDate(tab[0]),
					matchIntervalle(tab[1],INTERTEMPERATURE),
					matchIntervalle(tab[2],INTERHUMIDITE),
					matchIntervalle(tab[3],INTERPRESSION),
					matchIntervalle(tab[4],INTERPLUIE),
					matchIntervalle(tab[5],INTERVENT),
					extractDirection(tab[6]),
					matchIntervalle(tab[7],INTERROSEE),
					matchIntervalle(tab[8],INTERTEMPERATURE),
					matchIntervalle(tab[9],INTERRAFALES)
			);
			return j;
		} else {
			System.out.println("createJour error");
			return null;
		}
	}

	private static Date extractDate(String s){
		try{
			return df.parse(s);
		}catch(ParseException pe){
			System.out.println("extractDate Error");
			return null;
		}
	}

	private static int extractDirection(String s){
		if(s.compareTo("E") > 0) return 0;
		if(s.compareTo("E-NE") > 0 || s.compareTo("NE") > 0 || s.compareTo("N-NE") > 0) return 1;
		if(s.compareTo("N") > 0) return 2;
		if(s.compareTo("N-NO") > 0 || s.compareTo("NO") > 0 || s.compareTo("O-NO") > 0) return 3;
		if(s.compareTo("O") > 0) return 4;
		if(s.compareTo("O-SO") > 0 || s.compareTo("SO") > 0 || s.compareTo("S-SO") > 0) return 5;
		if(s.compareTo("S") > 0) return 6;
		if(s.compareTo("S-SE") > 0 || s.compareTo("SE") > 0 || s.compareTo("E-SE") > 0) return 7;
		System.out.println("extractDirection error ");
		return 8;
	}

	public static Intervalle matchIntervalle(String s, Intervalle[] inter){
		double n = Double.valueOf(s).doubleValue();
		for(int i = 0;i<inter.length;i++){
			if(n>=inter[i].start && n<inter[i].end && inter[i].inStart && !inter[i].inEnd) return inter[i];
			if(n>=inter[i].start && n<=inter[i].end && inter[i].inStart && inter[i].inEnd) return inter[i];
			if(n>inter[i].start && n<=inter[i].end && !inter[i].inStart && inter[i].inEnd) return inter[i];
			if(n>inter[i].start && n<inter[i].end && !inter[i].inStart && !inter[i].inEnd) return inter[i];
		}
		System.out.println("getIntervalle error");
		return null;
	}
}
